function AssignGenders(fin, fout, handles)

load(fin);

if isempty(selecttypesfilefull)
    selecttypesfilefull = 'selecttypes.mat';
end

load('selecttypes.mat');


pf_f = 0.8;
pf_m = 0.5;

pm_m = 0.90;
pm_f = 0.00001;

w1 = log(pf_m/pf_f);
w2 = log(pm_m/pm_f);  
sz = size(areascores);



len = sz(1);
ntypes = sz(3);

for i = 1:len
    
    nobjs = 0;
    
    bassigned = zeros(nfindobjs, 1);
    
    for j = 1:nfindobjs
        bunique = 1;
        
        if x1s(i,j) > 0
            
            for k = 1:nfindobjs
                if j ~= k
                    if x1s(i,j) == x1s(i,k) & x2s(i,j) == x2s(i,k) &...
                       y1s(i,j) == y1s(i,k) & y2s(i,j) == y2s(i,k) & (bassigned(j) == 1 || bassigned(k) == 1)
                          bunique = 0;
                    end
                end
            end
        else
            bunique = 0;
        end
        
        if bunique == 1
            nobjs = nobjs + 1;
            
            bassigned(j) = 1;
        end
    end
    
    objs(i) = nobjs;
        
end

nFemaletypeIndex = find(bFemales == 1 & bAssignGenders == 1);
nMaletypeIndex = find(bFemales == 0 & bAssignGenders == 1);

if isempty(nFemaletypeIndex) & isempty(nMaletypeIndex)
    return
end

bFemaleAreasDetected = 0;
bMaleAreasDetected = 0;

if ~isempty(nFemaletypeIndex)
    if detecttypes(nFemaletypeIndex) == 1
        bFemaleAreasDetected = 1;
    end
end

if ~isempty(nMaletypeIndex)
    if detecttypes(nMaletypeIndex) == 1
        bMaleAreasDetected = 1;
    end
end


alpha = 1/10000;
beta = 1/10000;

ymax = log((1-alpha)/alpha);
ymin = log(beta/(1-beta));

nstartindex = -1;

bgenderwarnings = zeros(len, 1);
gendertypes = -ones(len, nfindobjs);


if (bFemaleAreasDetected == 1 & bMaleAreasDetected == 0) | (bFemaleAreasDetected == 0 & bMaleAreasDetected == 1)
    
    for i = 1:len                
        if objs(i) == nfindobjs & i < len
            
            if nstartindex == -1
            
                y1 = zeros(nfindobjs, 1);
                y2 = zeros(nfindobjs, 1);
                
                nstartindex = i;
            end
        
            
            for j = 1:nfindobjs
                for k = 1:ntypes
                    if detecttypes(k) == 1 %this type of area was 
                    %selected by the user for detection (and has 
                    %hence been detected by the system)
                
                        if areasx1(i, j, k) > 0 %&bwarnings(i, j, k)==0
                            
                            if k == nFemaletypeIndex
                                
                                y1(j) = y1(j)+ w1;      %y1 contains "evidence" for labeling an object female
                                                                                
                            elseif k == nMaletypeIndex
                            
                                y2(j) = y2(j)+ w2;      %y2 contains "evidence" for labeling an object male
                                
                            end
                        end
                    end
                end
            end      
            
            
        elseif nstartindex ~= -1 %| i == len
            
            a = find(y1 > ymax);             
            lena = length(a);
            
            b = find(y2 > ymax);             
            lenb = length(b);
                        
            if lena > 1 | lenb > 1
                bgenderwarnings(i) = 1;                            
            end                        
                                    

            findex = -1;
            mindex = -1;
            
            for j = 1:nfindobjs                                
                                
                if (y1(j) < ymin || y1(j) > ymax) & y2(j) > ymin && y2(j) < ymax                    
                    
                    gendertypes(nstartindex:i-1, j) = 0; %set to female
                    
                    if nfindobjs == 2 & findex == -1
                        findex = j;
                    else
                        findex = -1000;
                    end
                elseif (y2(j) < ymin || y2(j) > ymax) & y1(j) > ymin && y1(j) < ymax   
                    
                    gendertypes(nstartindex:i-1, j) = 1; %set to male
                    
                    if nfindobjs == 2 & findex == -1
                        mindex = j;
                    else
                        mindex = -1000;
                    end
                end
            end

            
            
            if nfindobjs == 2
                if findex > 0 & mindex < 0
                    if findex == 1
                        mindex = 2;
                    else 
                        mindex = 1;
                    end
                       
                    gendertypes(nstartindex:i-1, mindex) = 1; %set to male
                elseif mindex > 0 & findex < 0
                    
                    if mindex == 1
                        findex = 2;
                    else 
                        findex = 1;
                    end
                    
                    gendertypes(nstartindex:i-1, findex) = 0; %set to female
                else
                    if y1(1) < ymin & y1(1) < y1(2)
                        gendertypes(nstartindex:i-1, 1) = 0; %set to female
                        gendertypes(nstartindex:i-1, 2) = 1; %set to male
                    elseif y1(1) > ymax & y1(1) > y1(2)
                        gendertypes(nstartindex:i-1, 1) = 0; 
                        gendertypes(nstartindex:i-1, 2) = 1; 
                    elseif y1(2) < ymin & y1(2) < y1(1)
                        gendertypes(nstartindex:i-1, 1) = 1; 
                        gendertypes(nstartindex:i-1, 2) = 0; 
                    elseif y1(2) > ymax & y1(2) > y1(1)
                        gendertypes(nstartindex:i-1, 1) = 1; 
                        gendertypes(nstartindex:i-1, 2) = 0; 
                    elseif y2(1) < ymin & y2(1) < y2(2)
                        gendertypes(nstartindex:i-1, 1) = 1; 
                        gendertypes(nstartindex:i-1, 2) = 0; 
                    elseif y2(1) > ymax & y2(1) > y2(2)
                        gendertypes(nstartindex:i-1, 1) = 1; 
                        gendertypes(nstartindex:i-1, 2) = 0; 
                    elseif y2(2) < ymin & y2(2) < y2(1)
                        gendertypes(nstartindex:i-1, 1) = 0; 
                        gendertypes(nstartindex:i-1, 2) = 1; 
                    elseif y2(2) > ymax & y2(2) > y2(1)
                        gendertypes(nstartindex:i-1, 1) = 0; 
                        gendertypes(nstartindex:i-1, 2) = 1; 
                    end
                        
                end
            end
                        

            nstartindex = -1;
        end
    end
    
    
end



save(fout, 'objs', 'x1s', 'x2s', 'y1s', 'y2s', 'ex0s', 'ey0s', 'ephis', 'eas', 'ebs', 'ets', ...
    'xheights', 'yheights', 'areas', 'nprocessstart', 'imgx', 'imgy', 'imghalfx', 'imghalfy', ...
    'areasx1', 'areasx2', 'areasy1', 'areasy2', 'areasx0', 'areasy0', 'areascores',...
    'nfindobjs', 'nfindmaxobjs', 'nstartframe', 'nendframe', ...
    'cdistmeans', 'cdiststds', 'scoremeans', 'scorestds', 'bestscores', 'bestscoreindexes',...
    'bwarnings', 'detecttypes', 'selecttypesfilefull', 'gendertypes', 'bgenderwarnings', 'objs1',...
    'bsegmented', 'dminscores', 'dmincdists');


